package znbkTopicType.resResponse;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;
import znbkTestPaperInfor.ZnbkPaperDef;

import static android.view.View.GONE;
import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;
import static znbkTestPaperRdtz.activity.HeatPointExtensionPaperActivity.mEntityRdtz;
import static znbkTestPaperSc.activity.CollectionPaperActivity.mEntitySc;

/**
 * Created by Administrator on 2018\3\18 0018.
 */

public class ZnbkResponseResStlTktAdapter extends BaseAdapter

{

    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int SumItem;
    //当前试题在当前试卷的ID
    private int PaperID;
    //当前问题小题所在大题的ID
    private int OptionID;
    private int ModelFrom;//试卷来源

    private int tvBlack;
    private int tvBlue;
    private int tvWhite;
    private int tvGreen;
    private int tvRed;


    private ViewHolder holder = null;
    private class ViewHolder {

        WebView WvContent;
        LinearLayout LayoutStuAnswerEdt;//输入区
        LinearLayout LayoutSeeAnalysis;//答案回看解析区
        TextView TvAnswerAmount;//小题总数

        LinearLayout mLayoutStuAnswer;//我的答案显示区
        LinearLayout mLayoutTvStuAnswer;//我的答案文字显示
        LinearLayout mLayoutAnswer;//正确答案显示区
        LinearLayout mLayoutTvAnswer;//正确答案文字显示
        LinearLayout mLayoutStuAssess;//自评显示区
        TextView mTvStuAssess;//自评文字显示
        LinearLayout mLayoutAnalysis;//解析显示区
        TextView mTvAnalysis;//解析文字显示

        private ViewHolder(View convertView) {
            WvContent = (WebView) convertView.findViewById(R.id.wv_content);
            LayoutStuAnswerEdt = (LinearLayout) convertView.findViewById(R.id.layout_stuAnswerEdt);
            LayoutSeeAnalysis = (LinearLayout) convertView.findViewById(R.id.layout_checkAnalysis);
            TvAnswerAmount = (TextView) convertView.findViewById(R.id.tv_answerAmount);

            mLayoutStuAnswer = (LinearLayout) convertView.findViewById(R.id.layout_stuAnswer);
            mLayoutTvStuAnswer = (LinearLayout) convertView.findViewById(R.id.layout_tvStuAnswer);
            mLayoutAnswer = (LinearLayout) convertView.findViewById(R.id.layout_answer);
            mLayoutTvAnswer = (LinearLayout) convertView.findViewById(R.id.layout_tvAnswer);
            mLayoutStuAssess = (LinearLayout) convertView.findViewById(R.id.layout_stuAssess);
            mTvStuAssess = (TextView) convertView.findViewById(R.id.tv_stuAssess);
            mLayoutAnalysis = (LinearLayout) convertView.findViewById(R.id.layout_analysis);
            mTvAnalysis = (TextView) convertView.findViewById(R.id.tv_analysis);

        }

    }

    /*
   * 获取小题选项信息
   * */
    private String SubChildAsk;//小题题干信息
    private String GenreName;//题材名称
    private String TypeName;//题型名称
    private String SubChildAnswer;//正确答案
    private String[] smallSubChildAnswer;
    private String SubChildAnalysis;//答案解析
    private int TotalItemCount;//小题数量
    public ZnbkResponseResStlTktAdapter(Context context, int sumItem, int paperID, int optionID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.SumItem = sumItem;
        this.PaperID = paperID;
        this.OptionID = optionID;
        this.ModelFrom = modelFrom;
    }

    @Override
    public int getCount() {
        return SumItem;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.znbk_adapter_response_res_tk_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
        * 获取作文题用户相关信息
        * */
        getInfor();

        /*
        * 显示题干信息
        * */
        showContent();

        /*
        * 显示用户回看解析相关信息
        */
        showAnalysis();

        return convertView;
    }


    /*
    * 获取作文题用户相关信息
    * */
    private void  getInfor() {

        tvBlack = mContext.getResources().getColor(R.color.black);
        tvBlue = mContext.getResources().getColor(R.color.blue);
        tvWhite = mContext.getResources().getColor(R.color.white);
        tvGreen = mContext.getResources().getColor(R.color.green);
        tvRed = mContext.getResources().getColor(R.color.red);

        if(ModelFrom== ZnbkPaperDef.FROM_HEAT_POINT_EXTENSION) {

                /*
                * 题材名称：GenreName
                * */
                    GenreName = mEntityRdtz.getResFromTK().getGenreName();
                /*
                * 题型名称：TypeName
                * */
                    TypeName = mEntityRdtz.getResFromTK().getTypeName();

                /*
                * 小题数量
                * */
                    TotalItemCount = mEntityRdtz.getResFromTK().getTotalItemCount();

                /*
                * 题干：SubChildAsk
                * */
                    if (GenreName.equals("选词填空")) {

                        String arraySubChildAsk[] = new String[TotalItemCount];
                        String subChildAskAll = "";
                        for (int i = 0; i < TotalItemCount; i++) {
                            arraySubChildAsk[i] = mEntityRdtz.getResFromTK().getChildList().get(i).getSubChildList().get(0).getSubChildAsk();
                            arraySubChildAsk[i].replace("____",
                                    "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                                            "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>");
                            subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                        }
                        SubChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);

                    } else {

                        SubChildAsk = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
                        String arraySubChildAsk[] = new String[TotalItemCount + 1];
                        String subChildAskAll = "";
                        for (int i = 0; i < TotalItemCount + 1; i++) {
                            arraySubChildAsk[i] = Control.returnArray(SubChildAsk, "____", 4)[i];
                            if (i < TotalItemCount) {
                                subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" +
                                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" +
                                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                            } else {
                                subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                            }
                        }
                        SubChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);
                    }

                /*
                * 答案：SubChildAnswer
                * 小题答案：smallSubChildAnswer[i]
                * */
                    if (GenreName.equals("选词填空")) {

                        smallSubChildAnswer = new String[TotalItemCount];
                        for (int i = 0; i < TotalItemCount; i++) {
                            smallSubChildAnswer[i] = mEntityRdtz.getResFromTK().getChildList().get(i).getSubChildList().get(0).getSubChildAnswer();
                        }
                    } else {

                        SubChildAnswer = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
                        smallSubChildAnswer = new String[TotalItemCount];
                        for (int i = 0; i < TotalItemCount; i++) {
                            smallSubChildAnswer[i] = Control.returnArray(SubChildAnswer, "$、", 2)[i];
                        }
                    }

                /*
                * 小题解析：SubChildAnalysis
                * */
                    if (GenreName.equals("选词填空")) {
                        String[] smallSubChildAnalysis = new String[TotalItemCount];
                        String subChildAnalysisAll = "";
                        for (int i = 0; i < TotalItemCount; i++) {
                            smallSubChildAnalysis[i] = mEntityRdtz.getResFromTK().getChildList().get(i).getSubChildList().get(0).getSubChildAnalyze();
                            subChildAnalysisAll = subChildAnalysisAll + smallSubChildAnalysis[i];
                        }
                        SubChildAnalysis = subChildAnalysisAll;
                    } else {

                        SubChildAnalysis = mEntityRdtz.getResFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();
                    }

        }else if(ModelFrom==ZnbkPaperDef.FROM_COLLECTION){

                    /*
                    * 题材名称：GenreName
                    * */
                    GenreName = mEntitySc.getResDetail().getGenreName();

                    /*
                    * 题型名称：TypeName
                    * */
                    TypeName = mEntitySc.getResDetail().getTypeName();

                    /*
                    * 小题数量
                    * */
                    TotalItemCount = mEntitySc.getResDetail().getTotalItemCount();

                    /*
                    * 题干：SubChildAsk
                    * */
                    if (GenreName.equals("选词填空")) {

                        String arraySubChildAsk[] = new String[TotalItemCount];
                        String subChildAskAll = "";
                        for (int i = 0; i < TotalItemCount; i++) {
                            arraySubChildAsk[i] = mEntitySc.getResDetail().getChildList().get(i).getSubChildList().get(0).getSubChildAsk();
                            arraySubChildAsk[i].replace("____",
                                    "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                                            "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>");
                            subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                        }
                        SubChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);

                    } else {

                        SubChildAsk = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
                        String arraySubChildAsk[] = new String[TotalItemCount + 1];
                        String subChildAskAll = "";
                        for (int i = 0; i < TotalItemCount + 1; i++) {
                            arraySubChildAsk[i] = Control.returnArray(SubChildAsk, "____", 4)[i];
                            if (i < TotalItemCount) {
                                subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" +
                                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" +
                                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                            } else {
                                subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                            }
                        }
                        SubChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);
                    }

                    /*
                    * 答案：SubChildAnswer
                    * 小题答案：smallSubChildAnswer[i]
                    * */
                    if (GenreName.equals("选词填空")) {

                        smallSubChildAnswer = new String[TotalItemCount];
                        for (int i = 0; i < TotalItemCount; i++) {
                            smallSubChildAnswer[i] = mEntitySc.getResDetail().getChildList().get(i).getSubChildList().get(0).getSubChildAnswer();
                        }
                    } else {

                        SubChildAnswer = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
                        smallSubChildAnswer = new String[TotalItemCount];
                        for (int i = 0; i < TotalItemCount; i++) {
                            smallSubChildAnswer[i] = Control.returnArray(SubChildAnswer, "$、", 2)[i];
                        }
                    }

                    /*
                    * 小题解析：SubChildAnalysis
                    * */
                    if (GenreName.equals("选词填空")) {
                        String[] smallSubChildAnalysis = new String[TotalItemCount];
                        String subChildAnalysisAll = "";
                        for (int i = 0; i < TotalItemCount; i++) {
                            smallSubChildAnalysis[i] = mEntitySc.getResDetail().getChildList().get(i).getSubChildList().get(0).getSubChildAnalyze();
                            subChildAnalysisAll = subChildAnalysisAll + smallSubChildAnalysis[i];
                        }
                        SubChildAnalysis = subChildAnalysisAll;
                    } else {

                        SubChildAnalysis = mEntitySc.getResDetail().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();
                    }
        }else if(ModelFrom==ZnbkPaperDef.FROM_NOTE){

                    /*
                    * 题材名称：GenreName
                    * */
            GenreName = mEntityBj.getModelTKQue().getGenreName();

                    /*
                    * 题型名称：TypeName
                    * */
            TypeName = mEntityBj.getModelTKQue().getTypeName();

                    /*
                    * 小题数量
                    * */
            TotalItemCount = mEntityBj.getModelTKQue().getTotalItemCount();

                    /*
                    * 题干：SubChildAsk
                    * */
            if (GenreName.equals("选词填空")) {

                String arraySubChildAsk[] = new String[TotalItemCount];
                String subChildAskAll = "";
                for (int i = 0; i < TotalItemCount; i++) {
                    arraySubChildAsk[i] = mEntityBj.getModelTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildAsk();
                    arraySubChildAsk[i].replace("____",
                            "<font color='#3399cc'><middle><middle><U>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                                    "（" + (i + 1) + "）" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>");
                    subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                }
                SubChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);

            } else {

                SubChildAsk = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
                String arraySubChildAsk[] = new String[TotalItemCount + 1];
                String subChildAskAll = "";
                for (int i = 0; i < TotalItemCount + 1; i++) {
                    arraySubChildAsk[i] = Control.returnArray(SubChildAsk, "____", 4)[i];
                    if (i < TotalItemCount) {
                        subChildAskAll = subChildAskAll + arraySubChildAsk[i] + "<font color='#3399cc'><middle><middle><U>" +
                                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "（" + (i + 1) + "）" +
                                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</U></middle></middle></font>";
                    } else {
                        subChildAskAll = subChildAskAll + arraySubChildAsk[i];
                    }
                }
                SubChildAsk = StringEscapeUtils.unescapeXml(subChildAskAll);
            }

                    /*
                    * 答案：SubChildAnswer
                    * 小题答案：smallSubChildAnswer[i]
                    * */
            if (GenreName.equals("选词填空")) {

                smallSubChildAnswer = new String[TotalItemCount];
                for (int i = 0; i < TotalItemCount; i++) {
                    smallSubChildAnswer[i] = mEntityBj.getModelTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildAnswer();
                }
            } else {

                SubChildAnswer = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
                smallSubChildAnswer = new String[TotalItemCount];
                for (int i = 0; i < TotalItemCount; i++) {
                    smallSubChildAnswer[i] = Control.returnArray(SubChildAnswer, "$、", 2)[i];
                }
            }

                    /*
                    * 小题解析：SubChildAnalysis
                    * */
            if (GenreName.equals("选词填空")) {
                String[] smallSubChildAnalysis = new String[TotalItemCount];
                String subChildAnalysisAll = "";
                for (int i = 0; i < TotalItemCount; i++) {
                    smallSubChildAnalysis[i] = mEntityBj.getModelTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildAnalyze();
                    subChildAnalysisAll = subChildAnalysisAll + smallSubChildAnalysis[i];
                }
                SubChildAnalysis = subChildAnalysisAll;
            } else {

                SubChildAnalysis = mEntityBj.getModelTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAnalyze();
            }
        }

    }

    /*
   * 获取题目题干信息
   * */
    private void showContent(){

        holder.TvAnswerAmount.setText("本题包含"+TotalItemCount+"个小题");
        if(TypeName.equals("阅读填空")) {
            holder.WvContent.loadDataWithBaseURL("about:blank", SubChildAsk, "text/html", "utf-8", null);
            holder.WvContent.getSettings().setDefaultFontSize(18);
        }

    }


    private TextView[] TvAnswer;
    /*
    * 显示试题解析回看信息
    * */
    private void showAnalysis(){

        TvAnswer = new TextView[TotalItemCount];

        //隐藏作答区域
        holder.LayoutStuAnswerEdt.setVisibility(GONE);
        //显示题目回看解析内容
        holder.LayoutSeeAnalysis.setVisibility(View.VISIBLE);
        //隐藏我的答案
        holder.mLayoutStuAnswer.setVisibility(GONE);
        //显示参考答案
        for(int i=0;i<TotalItemCount;i++){
            TvAnswer[i] = new TextView(mContext);
            TvAnswer[i].setText(Html.fromHtml(String.valueOf(i+1)+". "+smallSubChildAnswer[i]));
            TvAnswer[i].setTextSize(16);
            TvAnswer[i].setTextColor(tvGreen);
            holder.mLayoutTvAnswer.addView(TvAnswer[i]);
        }
        //隐藏自评
        holder.mLayoutStuAssess.setVisibility(GONE);
        //显示答案解析
        if (!SubChildAnalysis.equals("")) {
            holder.mTvAnalysis.setText(Html.fromHtml(SubChildAnalysis));
        } else {
            holder.mTvAnalysis.setText(R.string.znbk_no_analysis);
        }

    }

}